<?php
declare (strict_types = 1);

namespace app\common\model;

use think\Request;
use think\Model;
use think\model\concern\SoftDelete;
use app\common\model\AdminZbuserinfo as M;
use app\common\model\AdminZavote as MA;
use app\common\model\AdminZegzhpz as ME;
use app\common\model\AdminZitjxx as MI;
use think\facade\Db;
use think\model\relation\HasOne;

class AdminZbuserinfo extends Model
{
    use SoftDelete;
     protected $deleteTime = false;

    // 获取列表
    public static function getList($biaoid)
    {
        $where = [];
        $limit = input('get.limit');
               //按名称查找
               if ($uid = input("uid")) {
                   $where[] = ["uid", "like",  $uid ];
               }
               //按编号查找
               if ($biaoid) {
                    $where[] = ["biaoid", "like",  $biaoid ];
                }
        $list = Db::name('admin_zbuserinfo')->order('id','desc')->where($where)->paginate($limit);
        $res = MA::find($biaoid);
        if($res['shuxing'] == 1){
            $shuxing = explode(',',$res['shuxingzhi']);
            $shuxing = array_filter($shuxing);
            foreach ($shuxing as $k => $v) {
                $shuxingzhi = ME::find($v);
                $shuxing[$k] = $shuxingzhi['appsecret'];
            }
            $newlist=$list->items();
            foreach ($newlist as $kk => $vv) {
                foreach ($shuxing as $k => $v) {
                    $newlist[$kk][$v] = Db::name('admin_zitjxx')->where('biaoid', $vv['biaoid'])->where('utoken', $vv['uid'])->where('ziduanming', $v)->value('shuxingzhi');
                }
            }
        }else{
            $newlist=$list->items();
        }
        return ['code'=>0,'data'=>$newlist,'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }

    // 导出数据
    public static function goGetdata($biaoid)
    {
        $res = self::order('id','desc')->where('biaoid', $biaoid)->select();
        $restitle = MA::where('id', $biaoid)->field('title')->find();
        return ['msg'=>'操作成功','code'=>200,'data'=>$res,'datatitle'=>$restitle];
    }  

    public function getLaiyuanAttr($value)
    {
        $laiyuan = [0=>'微信',1=>'掌上邢台app',2=>'网页端'];
        return $laiyuan[$value];
    }

    // 记录用户信息
    public static function userinfo($token)
    {
        if(isset($token['uid'])){
            $w['biaoid']=$token['pid'];
            $w['uid']=$token['uid'];
            $w['laiyuan']=$token['laiyuan'];
            $isu = Db::name('admin_zbuserinfo')->where($w)->find();
            if(!$isu){
                $data['biaoid']=$token['pid'];
                $data['uid']=$token['uid'];
                $data['openid']=$token['uuname'];
                $data['nickname']=$token['unickname'];
                $data['headimgurl']=$token['uheadimgurl'];
                $data['laiyuan']=$token['laiyuan'];
                $data['ip'] =  getip();
                $data['datetime'] = date("Y-m-d H:i:s",time());
                $res=Db::name("admin_zbuserinfo")->insertGetId($data);
                if($res){
                    return ['code'=>200,'data'=>'记录用户成功'];
                }
            }else{
                return ['code'=>200,'data'=>'已注册'];
            }
        }else{
            return ['code'=>200,'data'=>'未登录状态'];
        }
    }

    // 报名获取
    public static function baoming($pid,$utoken)
    {
        $res = MA::find($pid);
        if($res['baoming'] == 1){
            $shuxing = explode(',',$res['bmxiang']);
            $shuxing = array_filter($shuxing);
            $shuxingxq = array();
            foreach ($shuxing as $k => $v) {
                $shuxingzhi = ME::find($v);
                $shuxingxq[$k]['value'] = "";
                $shuxingxq[$k]['name'] = $shuxingzhi['wxgzgname'];
                $shuxingxq[$k]['state'] = $shuxingzhi['appid'];
                $shuxingxq[$k]['ziduanming'] = $shuxingzhi['appsecret'];
                if($shuxingzhi['appid'] == 3){
                    $shuruxiang = explode('/',$shuxingzhi['shuruxiang']);
                    $shuruxiang = array_filter($shuruxiang);
                    for($i=0;$i < count($shuruxiang);$i++){
                        $shuxingxq[$k]['shuruxiang'][$i]['value'] = $i;
                        $shuxingxq[$k]['shuruxiang'][$i]['label'] = $shuruxiang[$i];
                    }
                    $shuxingxq[$k]['show'] = false;
                }
                if($shuxingzhi['appid'] == 2){
                    $shuxingxq[$k]['checked'] = false;
                    $shuruxiang = explode('/',$shuxingzhi['shuruxiang']);
                    $shuruxiang = array_filter($shuruxiang);
                    for($i=0;$i < count($shuruxiang);$i++){
                        $shuxingxq[$k]['shuruxiang'][$i]['name'] = $shuruxiang[$i];
                        $shuxingxq[$k]['shuruxiang'][$i]['checked'] = false;
                        $shuxingxq[$k]['shuruxiang'][$i]['disabled'] = false;
                    }
                }
                if($shuxingzhi['appid'] == 5){
                    $shuxingxq[$k]['diyu'] = json_decode($shuxingzhi['diyu']);
                }
                if($shuxingzhi['appid'] == 6){
                    if($res['bmnumimg'] != 0){
                        $shuxingxq[$k]['bmnumimg'] = $res['bmnumimg'];
                    }
                }
            }
            return ['code'=>200,'data'=>['shuxingxq' => $shuxingxq]];
        }else{
            return ['code'=>200,'data'=>'无'];
        }
    }

    // 属性获取
    public static function shuxing($pid,$utoken)
    {
        $w['utoken']=$utoken;
        $w['biaoid']=$pid;
        $isutoken = Db::name('admin_zitjxx')->where($w)->select();
        // return ['code'=>200,'data'=>$isutoken];
        // $isutoken = "1";
        if(count($isutoken) != 0){
            return ['code'=>200,'data'=>'已提交过信息'];
        }
        $res = MA::find($pid);
        if($res['shuxing'] == 1){
            $shuxing = explode(',',$res['shuxingzhi']);
            $shuxing = array_filter($shuxing);
            $shuxingxq = array();
            foreach ($shuxing as $k => $v) {
                $shuxingzhi = ME::find($v);
                $shuxingxq[$k]['value'] = "";
                $shuxingxq[$k]['name'] = $shuxingzhi['wxgzgname'];
                $shuxingxq[$k]['state'] = $shuxingzhi['appid'];
                $shuxingxq[$k]['ziduanming'] = $shuxingzhi['appsecret'];
                if($shuxingzhi['appid'] == 3){
                    $shuruxiang = explode('/',$shuxingzhi['shuruxiang']);
                    $shuruxiang = array_filter($shuruxiang);
                    for($i=0;$i < count($shuruxiang);$i++){
                        $shuxingxq[$k]['shuruxiang'][$i]['value'] = $i;
                        $shuxingxq[$k]['shuruxiang'][$i]['label'] = $shuruxiang[$i];
                    }
                    $shuxingxq[$k]['show'] = false;
                }
                if($shuxingzhi['appid'] == 2){
                    $shuxingxq[$k]['checked'] = false;
                    $shuruxiang = explode('/',$shuxingzhi['shuruxiang']);
                    $shuruxiang = array_filter($shuruxiang);
                    for($i=0;$i < count($shuruxiang);$i++){
                        $shuxingxq[$k]['shuruxiang'][$i]['name'] = $shuruxiang[$i];
                        $shuxingxq[$k]['shuruxiang'][$i]['checked'] = false;
                        $shuxingxq[$k]['shuruxiang'][$i]['disabled'] = false;
                    }
                }
                if($shuxingzhi['appid'] == 5){
                    $shuxingxq[$k]['diyu'] = json_decode($shuxingzhi['diyu']);
                }
            }
            // $shuxingxqimg = array();
            // if($res['bmnumimg'] != 0){
            //     $shuxingxqimg['value'] = "";
            //     $shuxingxqimg['name'] = "上传图片";
            //     $shuxingxqimg['state'] = "100";
            //     $shuxingxqimg['ziduanming'] = "shuxingxqimg";
            //     $shuxingxqimg['bmnumimg'] = $res['bmnumimg'];
            //     array_push($shuxingxq,$shuxingxqimg);
            // }
            return ['code'=>200,'data'=>['shuxingxq' => $shuxingxq]];
        }else{
            return ['code'=>200,'data'=>'无'];
        }
    }
}
